package com.teacold.customdemo.chapter_five;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.PathMeasure;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;

public class PathMeasureView extends View {
    private Paint mPaint;

    public PathMeasureView(Context context) {
        super(context);
        setLayerType(LAYER_TYPE_SOFTWARE,null);
        mPaint = new Paint();
        mPaint.setColor(Color.BLUE);
        mPaint.setStrokeWidth(5);
        mPaint.setStyle(Paint.Style.STROKE);
    }

    public PathMeasureView(Context context, AttributeSet attrs) {
        this(context);
    }

    public PathMeasureView(Context context, AttributeSet attrs, int defStyleAttr) {
        this(context, attrs);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        getSegment(canvas);

    }


    private void getSegment(Canvas canvas){
        canvas.translate(100,100);
        Path path=new Path();
        path.addRect(-50,-50,50,50,Path.Direction.CW);
        Path dst=new Path();
        PathMeasure measure=new PathMeasure(path,false);
        measure.getSegment(0,150,dst,true);
        canvas.drawPath(dst,mPaint);
    }


    private void nextContour(Canvas canvas) {
      canvas.translate(150,150);
      Path path=new Path();
        path.addRect(-100,-100,100,100,Path.Direction.CW);
      path.addRect(-50,-50,50,50,Path.Direction.CW);
      path.addRect(-120,-120,120,120,Path.Direction.CW);
      canvas.drawPath(path,mPaint);
      PathMeasure measure=new PathMeasure(path,false);
      do {
          float len=measure.getLength();
          Log.e("qijian","len="+len);
      }while (measure.nextContour());



    }
    private void MeasureView(Canvas canvas) {
        canvas.translate(50, 50);
        Path path = new Path();
        path.moveTo(0, 0);
        path.lineTo(0, 100);
        path.lineTo(100, 100);
        path.lineTo(100, 0);
        path.lineTo(0, 0);
        PathMeasure pathMeasure = new PathMeasure(path, false);
        PathMeasure pathMeasure1 = new PathMeasure(path, true);
        Log.e("qijian", "forceClosed=false-->" + pathMeasure.getLength());
        Log.e("qijian", "forceClosed=true-->" + pathMeasure1.getLength());
        canvas.drawPath(path, mPaint);
    }

}
